﻿using System;
using System.Collections;
using System.Collections.Generic;
using GameFramework;
using UnityEngine;

namespace StarForce {
    public class PlayerHand : CardPile {
        /// <summary>
        /// 目前选中的卡牌
        /// </summary>
        public Card SelectedCard { get; private set; }
        private event Action<Card> onCardSelect = card => { };
        private event Action<Card> onCardPlay = card => { };
        private event Action onCardUnSelect = () => { };

        /// <summary>
        /// 当卡牌被选中时的回调函数
        /// </summary>
        public Action<Card> OnCardSelect { 
            get => onCardSelect;
            set => onCardSelect = value;
        }
        /// <summary>
        /// 当使用卡牌的回调函数
        /// </summary>
        public Action<Card> OnCardPlay {
            get => onCardPlay;
            set => onCardPlay = value;
        }
        /// <summary>
        /// 当取消选择这张卡
        /// </summary>
        public Action OnCardUnSelect {
            get => onCardUnSelect;
            set => onCardUnSelect = value;
        }

        /// <summary>
        /// 选择一张卡牌
        /// </summary>
        /// <param name="card"></param>
        public void SelectCard(Card card) {
            SelectedCard = card ?? throw new ArgumentNullException("Null is not a valid argument.");
            NotifyCardSelect();
        }

        /// <summary>
        /// 使用这张卡片
        /// </summary>
        /// <param name="card"></param>
        public void PlayCard(Card card) {
            if (card == null) {
                throw new ArgumentNullException("Null is not a valid argument.");
            }

            SelectedCard = null;
            RemoveCard(card);
            onCardPlay?.Invoke(card);
            NotifyPileChange();    //重排
        }

        public void UnSelectCard(Card card) {
            if (card == null) {
                throw new ArgumentNullException("Null is not a valid argument.");
            }
            
            SelectedCard = null;
            NotifyPileChange();
            NotifyCardUnSelect();
        }

        /// <summary>
        /// 使用当前选择的卡片
        /// </summary>
        /// <exception cref="ArgumentException"></exception>
        public void PlaySelect() {
            if (SelectedCard == null) {
                throw new ArgumentNullException("Null is not a valid argument.");
            }
            PlayCard(SelectedCard);
        }

        /// <summary>
        /// 将卡牌放回手牌中
        /// </summary>
        /// <exception cref="ArgumentNullException"></exception>
        public void Unselect() {
            if (SelectedCard == null) {
                throw new ArgumentNullException("Null is not a valid argument.");
            }
            UnSelectCard(SelectedCard);
        }

        public void NotifyCardSelect() => onCardSelect?.Invoke(SelectedCard);
        public void NotifyCardUnSelect() => onCardUnSelect?.Invoke();
    }

}
